package cn.iocoder.yudao.module.member.service.usercreativeremark;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.usercreativeremark.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercreativeremark.UserCreativeRemarkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.usercreativeremark.UserCreativeRemarkMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 创意文案关联评论 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCreativeRemarkServiceImpl implements UserCreativeRemarkService {

    @Resource
    private UserCreativeRemarkMapper userCreativeRemarkMapper;

    @Override
    public Long createUserCreativeRemark(UserCreativeRemarkSaveReqVO createReqVO) {
        // 插入
        UserCreativeRemarkDO userCreativeRemark = BeanUtils.toBean(createReqVO, UserCreativeRemarkDO.class);
        userCreativeRemarkMapper.insert(userCreativeRemark);
        // 返回
        return userCreativeRemark.getId();
    }

    @Override
    public void updateUserCreativeRemark(UserCreativeRemarkSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCreativeRemarkExists(updateReqVO.getId());
        // 更新
        UserCreativeRemarkDO updateObj = BeanUtils.toBean(updateReqVO, UserCreativeRemarkDO.class);
        userCreativeRemarkMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCreativeRemark(Long id) {
        // 校验存在
        validateUserCreativeRemarkExists(id);
        // 删除
        userCreativeRemarkMapper.deleteById(id);
    }

    private void validateUserCreativeRemarkExists(Long id) {
        if (userCreativeRemarkMapper.selectById(id) == null) {
            throw exception(USER_CREATIVE_REMARK_NOT_EXISTS);
        }
    }

    @Override
    public UserCreativeRemarkDO getUserCreativeRemark(Long id) {
        return userCreativeRemarkMapper.selectById(id);
    }

    @Override
    public PageResult<UserCreativeRemarkDO> getUserCreativeRemarkPage(UserCreativeRemarkPageReqVO pageReqVO) {
        return userCreativeRemarkMapper.selectPage(pageReqVO);
    }

}